package com.it.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.reggie.common.Result;
import com.it.reggie.entity.Employee;
import com.it.reggie.service.IEmployeeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
@Api(tags = "员工管理")
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    IEmployeeService employeeService;

    /**
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        String passwordmd5 = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        if (emp==null){
            return Result.filed("该账号不存在");
        }
        if (emp.getStatus()!=1){
            return Result.filed("该账号已禁用");
        }
        if (emp.getPassword().equals(passwordmd5)){
            request.getSession().setAttribute("employee",emp.getId());
            return Result.success(emp);

        }else {
            return Result.filed("密码错误");
        }

    }

    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return Result.success("null");
    }

    @GetMapping("/page")
    public Result<Page> queryPage(Integer page,Integer pageSize,String name){
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        Page<Employee> employeePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null&&name!="", Employee::getName, name);
        employeeService.page(employeePage, wrapper);
        return Result.success(employeePage);

    }

    @PostMapping
    public Result save(HttpServletRequest request,@RequestBody Employee employee){
        String s="1234567";
        String password = DigestUtils.md5DigestAsHex(s.getBytes());
        employee.setPassword(password);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //获取当前登录账号id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        boolean save = employeeService.save(employee);
        return save?Result.success(null):Result.filed("添加失败");
    }

    @PutMapping
    public Result update(HttpServletRequest request,@RequestBody Employee employee){
        //获取当前登录账号id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);
        boolean update = employeeService.updateById(employee);
        return update?Result.success(null):Result.filed("添加失败");
    }

    @GetMapping("/{id}")
    public Result<Employee> queryOne(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return employee!=null?Result.success(employee):Result.filed("未查询到对应的数据");
    }
}
